/**
 * 窗口操作工具补丁
 * @author huyunming
 * @createTime 20160710
 */

Ext.apply(Ext.Window.prototype,{
	afterShow : function(){
        this.proxy.hide();
        this.el.setStyle('display', 'block');
        this.el.show();
        if(this.maximized){
            this.fitContainer();
            // 添加代码，在window显示时，判断最大化状态，添加css
            if(!this.container.hasClass('x-window-maximized-ct')){
            	this.container.addClass('x-window-maximized-ct');
            }
            if(!this.el.hasClass('x-window-maximized')){
        		this.el.addClass('x-window-maximized');
        	}
            // --
        }
        if(this.minimized){
        	if(this.container.hasClass('x-window-maximized-ct')){
            	this.container.removeClass('x-window-maximized-ct');
            }
        	if(this.el.hasClass('x-window-maximized')){
        		this.el.removeClass('x-window-maximized');
        	}
        	if(this.restorePos){
        		this.setPosition(this.restorePos[0], this.restorePos[1]);
        	}
        	if(this.restoreSize){
        	 	this.setSize(this.restoreSize.width, this.restoreSize.height);
        	}
	       this.tools.restore.hide();
	       this.tools.minimize.show();
	       this.tools.maximize.show();
	       var els=Ext.query('span',this.header.dom);
	       var element=Ext.get(els[0]);
		   element.setStyle('width','auto');
		   this.minimized=false;
        }
        if(Ext.isMac && Ext.isGecko){ // work around stupid FF 2.0/Mac scroll bar bug
        	this.cascade(this.setAutoScroll);
        }
        if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
            Ext.EventManager.onWindowResize(this.onWindowResize, this);
        }
        this.doConstrain();
        if(this.layout){
            this.doLayout();
        }
        if(this.keyMap){
            this.keyMap.enable();
        }
        this.toFront();
        this.updateHandles();
        this.fireEvent("show", this);
    },
	maximize : function() {
        if(!this.maximized ) {
            this.expand(false);
            if(!this.minimized) {
            	this.restoreSize = this.getSize();
            	this.restorePos = this.getPosition(true);
            }
            if (this.maximizable) {
                this.tools.maximize.hide();
                this.tools.restore.show();
                if(this.minimizable) {
                  this.tools.minimize.show();
                }
            }
            this.maximized = true;
            this.minimized = false;
            this.el.disableShadow();
            if(this.dd) {
                this.dd.lock();
            }
            if(this.collapsible) {
                this.tools.toggle.hide();
            }
            var els=Ext.query('span',this.header.dom);
	      	var element=Ext.get(els[0]);
			element.setStyle('width','auto');
            this.el.addClass('x-window-maximized');
            this.container.addClass('x-window-maximized-ct');
            // 添加代码，窗口定位
            var diffY = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
    			diffY = document.documentElement.scrollTop;
    		} else if (document.body) {
    			diffY = document.body.scrollTop
    		} else{
    			// other condition
    		}
            this.setPosition(0, diffY);
            // --
            this.fitContainer();
            this.fireEvent('maximize', this);
        }
    },
    minimize : function() {
		if (!this.minimized) {
			if (!this.maximized) {
				this.restoreSize = this.getSize();
				this.restorePos = this.getPosition(true);
			} else {
				this.el.removeClass("x-window-maximized");
				// 修复最大化，再最小化，父窗口滚动条消失的BUG
				this.container.removeClass('x-window-maximized-ct');
				// --
			}
			this.collapse(false);
			if (this.minimizable) {
				this.tools.minimize.hide();
				this.tools.restore.show();
				if (this.maximizable) {
					this.tools.maximize.show();
				}
			}
			this.minimized = true;
			this.maximized = false;
			this.el.disableShadow();
			if (this.dd) {
				this.dd.lock();
			}
			if (this.collapsible) {
				this.tools.toggle.show();
			}
			var els = Ext.query("span", this.header.dom);
			var element = Ext.get(els[0]);
			element.setStyle("width", "auto");
			var vs = this.container.getViewSize();
			var minWidth = 100+(this.title.length)*15;
			this.setPosition(vs.width - minWidth, 0);
			this.setSize(minWidth, 20);
			this.fireEvent("minimize", this);
			window.scrollTo(0, 0);
		}
	},
	restore : function() {
		this.tools.restore.hide();
		var els = Ext.query("span", this.header.dom);
		var element = Ext.get(els[0]);
		element.setStyle("width", "auto");
		if (this.maximized) {
			this.el.removeClass("x-window-maximized");
			this.container.removeClass("x-window-maximized-ct");
			this.tools.maximize.show();
			this.setPosition(this.restorePos[0], this.restorePos[1]);
			this.setSize(this.restoreSize.width, this.restoreSize.height);
			delete this.restorePos;
			delete this.restoreSize;
			this.maximized = false;
			this.el.enableShadow(true);
			if (this.dd) {
				this.dd.unlock();
			}
			if (this.collapsible) {
				this.tools.toggle.show();
			}
		} else if (this.minimized) {
			this.tools.minimize.show();
			this.setPosition(this.restorePos[0], this.restorePos[1]);
			this.setSize(this.restoreSize.width, this.restoreSize.height);
			delete this.restorePos;
			delete this.restoreSize;
			this.minimized = false;
			this.el.enableShadow(true);
			this.expand(false);
			if (this.maximizable) {
				this.tools.maximize.show();
			}
			if (this.dd) {
				this.dd.unlock();
			}
			if (this.collapsible) {
				this.tools.toggle.hide();
			}
		}
		this.doConstrain();
		this.fireEvent("restore", this);
		this.center();
	},
    hide : function(animateTarget, cb, scope){
        if(this.activeGhost){
            this.hide.defer(100, this, [animateTarget, cb, scope]);
            return;
        }
        if(this.hidden || this.fireEvent("beforehide", this) === false){
            return;
        }
        if(cb){
            this.on('hide', cb, scope, {single:true});
        }
        this.hidden = true;
        
        // 关闭window时，移除css
        if(this.container.hasClass('x-window-maximized-ct')){
        	this.container.removeClass('x-window-maximized-ct');
        }
        if(this.el.hasClass('x-window-maximized')){
    		this.el.removeClass('x-window-maximized');
    	}
        // --
        
        if(animateTarget !== undefined){
            this.setAnimateTarget(animateTarget);
        }
        if(this.animateTarget){
            this.animHide();
        }else{
            this.el.hide();
            this.afterHide();
        }
	}
});
